//
//  PassWordView.m
//  AnZMaster
//
//  Created by 刘萍 on 2018年4月11日 星期三.
//  Copyright © 2018年 MR_李胖子. All rights reserved.
//

#import "PassWordView.h"


#define UpPW @"/interface.php/v1/member/verify_second_pw"

#define kDotSize CGSizeMake (10, 10) //密码点的大小
#define kDotCount 6  //密码个数
#define K_Field_Height self.PWTextField.bounds.size.height  //每一个输入框的高度等于当前view的高度
@interface PassWordView ()<UITextFieldDelegate>

@property (nonatomic, strong) NSMutableArray *dotArray; //用于存放黑色的点点
@property (strong, nonatomic) MBProgressHUD *hud;

@end
@implementation PassWordView

- (void)CreatePassWordView{
    
    UITapGestureRecognizer * VTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(DisMisssTheView)];
    [self addGestureRecognizer:VTap];
    _PWTextField.backgroundColor = [UIColor whiteColor];
    [_PWTextField becomeFirstResponder];
    //输入的文字颜色为白色
    _PWTextField.textColor = [UIColor whiteColor];
    //输入框光标的颜色为白色
    _PWTextField.tintColor = [UIColor whiteColor];
    _PWTextField.delegate = self;
    _PWTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    _PWTextField.keyboardType = UIKeyboardTypeNumberPad;
    _PWTextField.layer.borderColor = [[UIColor grayColor] CGColor];
    _PWTextField.layer.borderWidth = 1;
    [_PWTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self initPwdTextField];
}

- (void)initPwdTextField
{
    //每个密码输入框的宽度
//    CGFloat width = self.PWTextField.bounds.size.width / kDotCount;
    CGFloat width = (KScreenW -32) / kDotCount;
    
    //生成分割线
    for (int i = 0; i < kDotCount - 1; i++) {
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.PWTextField.frame) + (i + 1) * width, CGRectGetMinY(self.PWTextField.frame)-1, 1, K_Field_Height)];
        lineView.backgroundColor = [UIColor grayColor];
        [self.PWBackView addSubview:lineView];
    }
    
    self.dotArray = [[NSMutableArray alloc] init];
    //生成中间的点
    for (int i = 0; i < kDotCount; i++) {
        UIView *dotView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.PWTextField.frame) + (width - kDotCount) / 2 + i * width, CGRectGetMinY(self.PWTextField.frame) + (K_Field_Height - kDotSize.height) / 2, kDotSize.width, kDotSize.height)];
        dotView.backgroundColor = [UIColor blackColor];
        dotView.layer.cornerRadius = kDotSize.width / 2.0f;
        dotView.clipsToBounds = YES;
        dotView.hidden = YES; //先隐藏
        [self.PWBackView addSubview:dotView];
        //把创建的黑色点加入到数组中
        [self.dotArray addObject:dotView];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"变化%@", string);
    if([string isEqualToString:@"\n"]) {
        //按回车关闭键盘
        [textField resignFirstResponder];
        return NO;
    } else if(string.length == 0) {
        //判断是不是删除键
        return YES;
    }
    else if(textField.text.length >= kDotCount) {
        //输入的字符个数大于6，则无法继续输入，返回NO表示禁止输入
        NSLog(@"输入的字符个数大于6，忽略输入");
        return NO;
    } else {
        return YES;
    }
}

/**
 *  清除密码
 */
- (void)clearUpPassword
{
    self.PWTextField.text = @"";
    [self textFieldDidChange:self.PWTextField];
}

/**
 *  重置显示的点
 */
- (void)textFieldDidChange:(UITextField *)textField
{
    NSLog(@"%@", textField.text);
    for (UIView *dotView in self.dotArray) {
        dotView.hidden = YES;
    }
    for (int i = 0; i < textField.text.length; i++) {
        ((UIView *)[self.dotArray objectAtIndex:i]).hidden = NO;
    }
    if (textField.text.length == kDotCount) {
        NSLog(@"输入完毕");
        [self.delegate PushPassWordWith:textField.text];
    }
}

- (IBAction)ChangePassWord:(id)sender {
    [self clearUpPassword];
    [self.delegate ChangeMyPassWord];
}
- (IBAction)MissPassWord:(id)sender {
    [self clearUpPassword];
    [self.delegate MissMyPassWord];
}

- (IBAction)ClickBtnDismissPassView:(id)sender {
    self.hidden = YES;
    [self endEditing:YES];
}

- (void)DisMisssTheView{
    self.hidden = YES;
    [self endEditing:YES];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
